-- ======================================================================
-- Hell Lua Bus
-- 
-- Copyright 2012-2013Hell-Prototypes
--
-- http://www.hellprototypes.com/
--
-- This is free software, licensed under the terms of the GNU General
-- Public License as published by the Free Software Foundation.
-- ======================================================================
hlb = require('libhlb')
printf = require "mod_print"

function device_open()
	local ret = hlb.usb_open(0x04B4FFFF)
	if ret < 0 then
		print("Fail to open usb device")
		return false
	end
	
	return true
end

function device_close()
	local ret = hlb.usb_close()
	if ret < 0 then
		print("Fail to close usb device")
		return false
	end
	
	return true
end

function pause()
    io.write("\r\nAny key to continue ...")
    io.flush()
    io.read()
    print("")
end

-- -----------------------------------------------
function send_ctrl_cmd(cmd_data)
    if type(cmd_data) ~= "table" then
        print("cmd_data must be table type")
        return false
    end

    local msg = {0x40, 0xb0, 0, 0, #cmd_data, 500}
    local ret

    if #cmd_data > 0 then
        ret = hlb.usb_control_msg(cmd_data, msg)
    else
        ret = hlb.usb_control_msg(msg)
    end
    
    if ret < 0 then
        print("send_ctrl_cmd() ret = " .. ret)
        return false
    end

    return true
end
-- -----------------------------------------------
function split_to_4byte(data)
	local B3 = bit32.band(bit32.rshift(data, 24), 0xFF)
	local B2 = bit32.band(bit32.rshift(data, 16), 0xFF)
	local B1 = bit32.band(bit32.rshift(data, 8),  0xFF)
	local B0 = bit32.band(data, 0xFF)
    
    return B3, B2, B1, B0
end

-- -----------------------------------------------
function set_data_length(len)
	cmd = {0x80} -- set data len
	cmd[2], cmd[3], cmd[4], cmd[5] = split_to_4byte(len)
	io.write("Set data length command: ")
	printf.table(cmd, false)
	return send_ctrl_cmd(cmd)
end
-- -----------------------------------------------
ret = device_open()
if ret == false then
    print("device_open fail")
    return
end

cmd = {0x3F}
ret = send_ctrl_cmd(cmd)
if ret == false then
    print("send_ctrl_cmd() exec fail 0")
	device_close()
    return
end
print("Start FPGA App OK")

hlb.msdelay(100) --wait for reconfig

cmd = {0x00} --Reset
ret = send_ctrl_cmd(cmd)
if ret == false then
    print("send_ctrl_cmd() exec fail 1")
	device_close()
    return
end
print("Reset FPGA OK")

local count = 10
while count > 0 do
	count = count -1
	print("############################## ".. count)
	local data_len = 64
	ret = set_data_length(data_len)
	if ret == false then
		print("send_ctrl_cmd() exec fail 2")
		device_close()
		return
	end
	print("Set data length to: " .. data_len * 4)
	hlb.msdelay(100)
	read_back = hlb.usb_bulk_read(0x86, data_len*4, 500)
	if type(read_back) == "number" then
		print(" ret = " .. read_back)
		break;
	else
		printf.table(read_back, true, 32)
	end

end
device_close()
